package com.snap.vseries.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Environment;
import android.view.View;

import com.snap.vseries.AppConfig;
import com.snap.vseries.bean.HoleInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CaptureUtil {

    /**
     * @param view     chartView实例
     * @param holeInfo
     * @param path
     */
    public static boolean saveImg(View view, String experimentName, long startTime, HoleInfo holeInfo, String path) {
        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        final Bitmap bitmap = view.getDrawingCache();
        try {
            return save(bitmap, experimentName, startTime, holeInfo, path);
        } finally {
            view.destroyDrawingCache();
        }
    }

    private static boolean save(Bitmap bitmap, String experimentName, long startTime, HoleInfo holeInfo, String path) {
        Bitmap bitmap1 = Bitmap.createBitmap(1266, 1070, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawColor(Color.WHITE);
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);


        mPaint.setTextSize(32);
        //黑色
        mPaint.setColor(0xFF333333);
        mPaint.setStrokeWidth(2);
        String tip = "Amplification Plot";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 297, 80, mPaint);

        mPaint.setColor(0xFF509CF9);
        canvas.drawRoundRect(new RectF(111, 116, 111 + 48, 116 + 16), 10, 10, mPaint);

        mPaint.setColor(0xFF63C03A);
        canvas.drawRoundRect(new RectF(278, 116, 278 + 48, 116 + 16), 10, 10, mPaint);

        mPaint.setColor(0xFFFFD40C);
        canvas.drawRoundRect(new RectF(445, 116, 445 + 48, 116 + 16), 10, 10, mPaint);

        mPaint.setColor(0xFFF95050);
        canvas.drawRoundRect(new RectF(612, 116, 612 + 48, 116 + 16), 10, 10, mPaint);

        mPaint.setTextSize(24);
        //黑色
        mPaint.setColor(0xFF2152A3);
        mPaint.setStrokeWidth(2);
        tip = "实验名称";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 818, 168 + 32, mPaint);
        tip = "实验时间";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 818, 303 + 32, mPaint);
        tip = "样本孔位";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 818, 438 + 32, mPaint);

        mPaint.setTextSize(32);
        //黑色
        mPaint.setColor(0xFF333333);
        mPaint.setStrokeWidth(1);
        tip = experimentName;
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 818, 221 + 38, mPaint);
        tip = AutoUtil.getDaySecond(new Date(startTime));
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 818, 356 + 38, mPaint);
        // TODO: 2019/9/3 形式待定
        tip = "S1;  S3-5;  S7;";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 818, 491 + 38, mPaint);

        mPaint.setTextSize(32);
        //黑色
        mPaint.setColor(0xFF666666);
        mPaint.setStrokeWidth(1);
        tip = "FAM";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 183, 104 + 30, mPaint);
        tip = "VIC";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 350, 104 + 30, mPaint);
        tip = "ROX";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 517, 104 + 30, mPaint);
        tip = "CY5";
        mPaint.getTextBounds(tip, 0, tip.length(), new Rect());
        canvas.drawText(tip, 684, 104 + 30, mPaint);

        mPaint.setColor(0xFF509CF9);
        mPaint.setStyle(Paint.Style.STROKE);
        Path linePath = new Path();
        mPaint.setPathEffect(new DashPathEffect(new float[]{10f, 5f}, 0));
        linePath.moveTo(80, 168);
        linePath.lineTo(80 + 672, 168);
        linePath.lineTo(80 + 672, 168 + 822);
        linePath.lineTo(80, 168 + 822);
        linePath.close();
        //虚线
        canvas.drawPath(linePath, mPaint);
        canvas.drawBitmap(bitmap,
                null,
                new Rect(80 + 4, 168 + 4, 80 + 672 - 4, 168 + 822 - 4),
                new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
        //保存
        canvas.save(Canvas.ALL_SAVE_FLAG);
        // 存储
        canvas.restore();
        return packagePath(bitmap1, path);
    }

    private static boolean packagePath(Bitmap bitmap, String path) {
        String imageDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA).format(new Date());
        String mFileName = String.format("Image%s", imageDate);
        String usbPath = path + File.separator + mFileName + ".jpg";
        String sdPath = Environment.getExternalStorageDirectory().getPath() +
                AppConfig.IMG_DIRECTORY + mFileName + ".jpg";
        boolean isUsbSuccess = saveBitmap(bitmap, usbPath);
        boolean isSdSuccess = saveBitmap(bitmap, sdPath);
        return isSdSuccess && isUsbSuccess;
    }

    /**
     * 保存截图
     */
    private static boolean saveBitmap(Bitmap bitmap, String path) {
        File file = new File(path);
        FileUtils.mkFile(file);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}